package com.gyj.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/*
 * AppContextHelper可以让我们根据class的名称，类型等信息，从springIOC容器中拿到对应的class实例
 */
@Component
public class AppContextHelper implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	public AppContextHelper() {
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		AppContextHelper.applicationContext = applicationContext;
	}

	public static Object getBean(String beanName) {
		return applicationContext != null ? applicationContext.getBean(beanName) : null;
	}

	// 通过class获取Bean.class的名称，类型等信息
	public static <T> T getBean(Class<T> clazz) {
		return applicationContext.getBean(clazz);
	}

}
